package com.bjsxt.shop.orders.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.goods.pojo.AUsersCar;
import com.bjsxt.shop.goods.service.IGoodsService;
import com.bjsxt.shop.orders.dao.IAOrdersDao;
import com.bjsxt.shop.orders.dao.IAOrdersGoodsDao;
import com.bjsxt.shop.orders.dao.IAOrdersHistoryDao;
import com.bjsxt.shop.orders.pojo.AOrders;
import com.bjsxt.shop.orders.pojo.AOrdersGoods;
import com.bjsxt.shop.orders.pojo.AOrdersHistory;
import com.bjsxt.shop.orders.service.IOrdersService;
import com.bjsxt.shop.users.pojo.AUsersAddress;
import com.bjsxt.shop.users.service.IUsersShopService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 订单的Service类
 * @author wangshSxt
 *
 */
@Service("ordersService")
public class OrdersServiceImpl implements IOrdersService
{
	@Resource
	private IAOrdersDao ordersDao ; 
	@Resource
	private IAOrdersGoodsDao ordersGoodsDao;
	@Resource
	private IAOrdersHistoryDao ordersHistoryDao;
	
	@Resource
	private IUsersShopService usersShopService;
	@Resource
	private IGoodsService goodsService;
	
	@Override
	public AOrders findOneOrdersService(Map<String, Object> condMap)
	{
		return this.ordersDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersService(AOrders orders)
	{
		JSONObject resultJSON = new JSONObject();
		
		/* 查询收货地址id对应的收货地址信息,
		 * 一一的为订单赋值 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", orders.getAddressId());
		AUsersAddress usersAddress = this.usersShopService.findOneUsersAddressService(condMap);
		if(usersAddress != null)
		{
			orders.setRegionId(usersAddress.getRegionId());
			orders.setContact(usersAddress.getContact());
			orders.setPhone(usersAddress.getPhone());
			orders.setAddress(usersAddress.getAddress());
			orders.setZipCode(usersAddress.getZipCode());
			
			/* 
			 * 用户原来的收货地址,都变成非默认
			 *  */
			condMap.clear();
			condMap.put("operType", "updateDef");
			condMap.put("isDef", "0");
			condMap.put("usersId", orders.getUsersId());
			/*这里讲usersshop里面的地址都变成非默认的情况			 */
			this.usersShopService.updateBatchUsersAddressService(condMap);
			
			/* 将用户选择的收货地址变成默认地址,其它的变成不默认地址
			 * 因为传递的是对象，所以会把该对象所有的属性全部传递过去 */
			usersAddress.setIsDef(Byte.valueOf("1"));
			this.usersShopService.updateOneUsersAddressService(usersAddress);
		}
		
		/* 影响条数 */
		int res = this.ordersDao.save(orders);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 保存订单商品 */
			for (Iterator iterator = orders.getOrdersGoodList().iterator(); iterator.hasNext();)
			{
				AOrdersGoods ordersGoods = (AOrdersGoods) iterator.next();
				/* 设置订单Id */
				ordersGoods.setOrdersId(orders.getId());
				this.saveOneOrdersGoodsService(ordersGoods);
				
				/* 要清空购物车 
				 * 将购物车里面的状态改为0,
				 * 根据用户Id和商品Id将状态改为0(表示清空购物车)
				 * */
				condMap.clear();
				condMap.put("usersId", orders.getUsersId());
				condMap.put("goodsId", ordersGoods.getGoodsId());
				AUsersCar usersCar = this.goodsService.findOneUsersCarService(condMap);
				if(usersCar != null)
				{
					usersCar.setStatus(Byte.valueOf("0"));
					usersCar.setUpdateTime(new Date());
					this.goodsService.updateOneUsersCarService(usersCar);
				}
			}
			
			/* 叫保存订单操作明细表 */
			AOrdersHistory ordersHistory = new AOrdersHistory();
			ordersHistory.setOrdersId(orders.getId());
			ordersHistory.setSn("");
			ordersHistory.setContent("您提交订单");
			ordersHistory.setStatus(Byte.valueOf("1"));
			ordersHistory.setCreateTime(new Date());
			this.saveOneOrdersHistoryService(ordersHistory);
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", orders.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersService(AOrders orders)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersDao.update(orders);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", orders.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersService(AOrders orders)
	{
		return null ;
	}
	
	@Override
	public Map<String, Object> findCondListOrdersService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AOrders> roleList = this.ordersDao.findList(condMap);
			/* 使用Dubbo的时候,不能为参数新增加赋值 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AOrders> roleList= this.ordersDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	}
	
	@Override
	public AOrdersGoods findOneOrdersGoodsService(Map<String, Object> condMap)
	{
		return this.ordersGoodsDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersGoodsDao.save(ordersGoods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersGoods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersGoodsDao.update(ordersGoods);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersGoods.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersGoodsService(AOrdersGoods ordersGoods)
	{
		return null ; 
	}
	
	@Override
	public Map<String, Object> findCondListOrdersGoodsService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AOrdersGoods> roleList = this.ordersGoodsDao.findList(condMap);
			/* 使用Dubbo的时候,不能为参数新增加赋值 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AOrdersGoods> roleList= this.ordersGoodsDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	}
	
	@Override
	public AOrdersHistory findOneOrdersHistoryService(Map<String, Object> condMap)
	{
		return this.ordersHistoryDao.findOne(condMap);
	}
	
	@Override
	public JSONObject saveOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersHistoryDao.save(ordersHistory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersHistory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("1"));
		}
		return resultJSON;
	}
	
	@Override
	public JSONObject updateOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		JSONObject resultJSON = new JSONObject();
		/* 影响条数 */
		int res = this.ordersHistoryDao.update(ordersHistory);
		/*String str = null ; 
		str.toString();*/
		if(res > 0 )
		{
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			
			/* 存储data */
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", ordersHistory.getId() + "");
			dataJSON.put("effect", res + "");
			
			resultJSON.put("data", dataJSON);
		}else
		{
			resultJSON.put("code", "2");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("2"));
		}
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneOrdersHistoryService(AOrdersHistory ordersHistory)
	{
		return null ; 
	}
	
	@Override
	public Map<String, Object> findCondListOrdersHistoryService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{

		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 为关键字加上%,实现模糊匹配 
		 * */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword")  + "%");
		}
		
		if(pageInfoUtil != null)
		{
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			/* 查询总记录数 */
			List<AOrdersHistory> roleList = this.ordersHistoryDao.findList(condMap);
			/* 使用Dubbo的时候,不能为参数新增加赋值 */
			pageInfoUtil.setTotalRecord(Long.valueOf(page.getTotal()).intValue());
			
			resultMap.put("list", roleList);
			resultMap.put("totalRecord", pageInfoUtil.getTotalRecord());
			return resultMap;
		}
		/* 不分页查询数据 */
		List<AOrdersHistory> roleList= this.ordersHistoryDao.findList(condMap);
		resultMap.put("list", roleList);
		return resultMap ; 
	
	}
}
